package com.ssh;

public class LongestPalindrome_5 {
	
	public static void main(String[] args) {
		String s = "babzzadadazz";
		System.out.println(longestPalindrome(s));
	}
	private static int lo = 0,hi = 0;
	private static int max = 0;
	
	public static String longestPalindrome(String s) {
		for (int i = 0; i < s.length(); i++) {
			lp(i, i, s);
			lp(i, i+1, s);
		}
		
		return s.substring(lo, hi);
	}
	private static void lp (int i,int j,String s) {
		while (i >= 0 && j <s.length() && s.charAt(i) == s.charAt(j)) {
			i--;
			j++;
		}
		if (max < j-i-1) {
			max = j-i-1;
			lo = i+1;
			hi = j;
		}
		
	}
	
	/*public static String longestPalindrome(String s) {
		String str = String.valueOf(s.charAt(0));
		int len = s.length();
		for (int i = 0; i < len; i++) {
			for (int j = len-1;j > i; j--) {
				if (s.charAt(i) == s.charAt(j)) {
					int z = 0;
					for (int x = i,y = j; x < (i+j+1)/2; x++,y--) {
						if (s.charAt(x) == s.charAt(y)) {
							z++;
						}
					}
					if ((j-i+1)/2 == z) {
						str = str.length() > s.substring(i, j+1).length() ? str : s.substring(i, j+1);
					}
				}
			}
		}
		return str;
	}*/
	/* public static String longestPalindrome(String s) {
		 String str = "";
		 int max = 0;
		 int len = s.length();
		 for (int i = 0; i < len-1; i++) {
			 for (int j = len; j > i+1; j--) {
				 StringBuffer x = new StringBuffer();
				 x.append(s.substring(i, j));
				 System.out.println("x="+x);
				 if (s.substring(i, j).equals(x.reverse().toString()) && max <(j-i)) {
					  max  = j-i;
					 str = s.substring(i, j);
				 }
			 }
		 }
		 if (max == 0) {
			 str = String.valueOf(s.charAt(0));
		 }
		 return str;
	 }*/
	 
	 
}
